/*
 * @features: 功能
 * @description: 说明
 * @Date: 2020-08-27 15:21:13
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2020-08-27 18:50:07
 * @LastEditors: judu233
 */

const { ccclass, property } = cc._decorator;

/**  
    * @Title : 负责缩放指定物体
    * @Description : 类说明
 **/
@ccclass
export default class CameraScaleContrl extends cc.Component {
    /**摄像机组件 */
    @property({
        type: cc.Camera,
        displayName: `缩放摄像机`,
        tooltip: `要显示的缩放摄像机,如果没有赋值，将查找本节点`,
    })
    public com_sclaeCamera: cc.Camera = null;

    /**播放组件 */
    @property({
        type: cc.Animation,
        displayName: `播放组件`,
        tooltip: `播放动画的anim组件，将查找本节点`,
    })
    public com_anim: cc.Animation = null;

    @property({
        type: cc.Float,
        displayName: `最小缩放`,
        tooltip: `摄像机最小缩放倍数`,
        min: 0.1,
    })
    public minScale: number = 1;
    @property({
        type: cc.Float,
        displayName: `最大缩放`,
        tooltip: `摄像机最大缩放倍数`,
        min: 0.1
    })
    public maxScale: number = 5;

    @property({
        type: cc.Node,
        displayName: `遮挡目标节点`,
        tooltip: `遮挡当放大时目标节点误触，防止事件穿透`,
    })
    public node_notTap: cc.Node = null;

    /**中心坐标节点·原点坐标 */
    @property({
        type: cc.Node,
        displayName: `原点节点`,
        tooltip: `摄像机缩小的目标节点。没有将设置摄像机所在节点`,
    })
    public node_center: cc.Node = null;
    /** 目标坐标节点 */
    public node_target: cc.Node;

    /**当前缩放状态 */
    public isEnlarge: boolean = false;

    /**放大时打开 */

    start() {
        //检查摄像机组件
        this.com_sclaeCamera = this.com_sclaeCamera ? this.com_sclaeCamera : this.getComponent(cc.Camera);
        this.com_anim = this.com_anim ? this.com_anim : this.getComponent(cc.Animation);
        this.node_center = this.node_center ? this.node_center : this.com_sclaeCamera?.node;
        if (!this.com_anim) cc.warn(`播放组件未挂载，请检查`);
        if (!this.node_center) cc.warn(`原点节点未挂载，请检查`);
        else {
            //设置相机初始位置
            let wordPos = this.node_center.convertToWorldSpaceAR(cc.v3(0, 0, 0));
            this.com_sclaeCamera.node.position = this.com_sclaeCamera.node.parent.convertToNodeSpaceAR(wordPos);
        }
        if (!this.com_sclaeCamera) cc.warn(`摄像机组件没有挂载，请检查挂载`);

        this.initCamera();
    }

    /**初始化摄像机 */
    public initCamera() {
    }

    /**放大摄像机*/
    public enlargeCamera(event: cc.Event) {
        let clickTarget: cc.Node = event.target;
        let wordPos = clickTarget.convertToWorldSpaceAR(cc.v2(0, 0));
        let targetPos = this.node_center.parent.convertToNodeSpaceAR(wordPos);
        //保存当前点击对象
        this.node_target = clickTarget;
        //放大
        this.zoomCamera(0, cc.v3(targetPos));
        //打开防止误触放大按钮
        this.node_notTap.active = true;
        this.isEnlarge = true;
    }

    /**缩小摄像机 */
    public shrinkCamera() {
        //缩小
        this.zoomCamera(1, this.node_center.position);
        //关闭
        this.node_notTap.active = false;
        this.isEnlarge = false;
    }

    /**
     * 放大缩小摄像机 
     * @param index  缩放序列 0放大，1缩小
     * @param targetPos 目标位置
     */
    private zoomCamera(index: number, targetPos: cc.Vec3) {
        let anim = this.com_anim;
        let animClip = anim.getClips()[index];
        let animState = anim.play(animClip.name);
        let nowPos = this.com_sclaeCamera.node.position;
        //设置动画参数
        animState.curves[0].values = [nowPos, targetPos];
        animState.curves[1].values[1] = (index == 0 ? this.maxScale : this.minScale);
    }
}
